package cn.aitstone.basic.controller;

import cn.aitstone.basic.dto.LoginDto;
import cn.aitstone.basic.dto.TokenDto;
import cn.aitstone.basic.exception.BusinessException;
import cn.aitstone.basic.service.ILoginService;
import cn.aitstone.basic.utils.AjaxResult;
import cn.aitstone.user.dto.WxDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){

        try {
            Map<String, Object> map = loginService.account(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
    * @Title: logout
    * @Description: 退出登录
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/8 21:28
    * @Parameters: [request]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/logout")
    public AjaxResult logout(@RequestBody String token){
        try {
            loginService.logout(token);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试");
        }
    }

    /**
    * @Title: sendMobileCode
    * @Description: 发送验证码接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/9 22:10
    * @Parameters: [loginDto]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/sendCode")
    public AjaxResult sendCode(@RequestBody LoginDto loginDto){
        try {
            loginService.sendCode(loginDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    /**
    * @Title: codeLogin
    * @Description: 通过验证码登录接口
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/9 22:11
    * @Parameters: [loginDto]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/codeLogin")
    public AjaxResult codeLogin(@RequestBody LoginDto loginDto){
        try {
            TokenDto tokenDto = loginService.codeLogin(loginDto);
            return AjaxResult.me().setResultObj(tokenDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }


    /**
    * @Title: wechat
    * @Description: 微信登录
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/15 12:02
    * @Parameters: [wxDto]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WxDto wxDto){
        try {
            return loginService.wechat(wxDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    /**
    * @Title: binder
    * @Description: 微信绑定
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/15 12:02
    * @Parameters: [wxDto]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WxDto wxDto){
        try {
            Map<String, Object> map = loginService.binder(wxDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }
}
